package pl.com.hme.kody.pocztowe.parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import pl.com.hme.kody.pocztowe.data.SearchResults;
import android.util.Log;

public class PocztaXmlParser {

    private static final String TAG = "PocztaXmlParser";

    private SearchResults mSearchResults = new SearchResults();
    
    public static void parseXml(InputStream inputStream) {
        HtmlCleaner pageParser = new HtmlCleaner();
        CleanerProperties props = pageParser.getProperties();
        props.setAllowHtmlInsideAttributes(true);
        props.setAllowMultiWordAttributes(true);
        props.setRecognizeUnicodeChars(true);
        props.setOmitComments(true);
        
        
        try {
            TagNode node = pageParser.clean(new InputStreamReader(inputStream));

            String xPathExpression = "//div[@id='zawartosc']/table[2]/tbody/tr/td";
            try {
                 // Stupid API returns Object[]...  Why not TagNodes?  We'll cast it later
                 Object[] downloadNodes = node.evaluateXPath(xPathExpression);
             

                 for(Object linkNode : downloadNodes){  

                 }  
            } catch (XPatherException e) {
                Log.e("ERROR", e.getMessage());
           }
        } catch (IOException e) {
            Log.w(TAG, "SAX parsing failed", e);
        }

    }
    
   /* public void characters(char[] ch, int start, int length) throws SAXException {
        PointOfInterest poi = new PointOfInterest();
        boolean codeWillOccur = false;
        boolean localityWillOccur = false;
        boolean boroughWillOccur = false;
        boolean districtWillOccur = false;
        boolean provinceWillOccur = false;
        boolean streetWillOccur = false;
        boolean streetNumbersWillOccur = false;
        String streetNumbersBuffer = "";
        
        //super.characters(ch, start, length);
        String text = new String(ch, start, length);
        //if (HomeActivity.DEBUG) Log.v(TAG, "Text is:" + text);

        if (codeWillOccur) {
            codeWillOccur = false;
            poi.setCode(text);
        }

        if (text.contains("kod (PNA):")) {
            codeWillOccur = true;
        }

        if (provinceWillOccur) {
            provinceWillOccur = false;
            poi.setProvince(text.toLowerCase());
        }

        if (text.contains("wojew�dztwo:")) {
            provinceWillOccur = true;
        }

        if (localityWillOccur) {
            localityWillOccur = false;
            poi.setLocality(text);
        }

        if (text.contains("miejscowo��:")) {
            localityWillOccur = true;
        }

        if (districtWillOccur) {
            districtWillOccur = false;
            poi.setDistrict(text);
        }

        if (text.contains("powiat:")) {
            districtWillOccur = true;
        }

        if (boroughWillOccur) {
            boroughWillOccur = false;
            poi.setBorough(text);
            
            // Borough is last entry - trying to add poi
            if(poi.isValid()){
                mSearchResults.addPoi(poi);
                poi = new PointOfInterest();
            }
        }

        if (text.contains("gmina:")) {
            boroughWillOccur = true;
        }

        if (streetWillOccur && trimIncludingNonbreakingSpace(text).length() > 0) {
            streetWillOccur = false;
            poi.setStreet(text.trim());
        }

        if (text.contains("ulica:")) {
            streetWillOccur = true;
        }

        if (streetNumbersWillOccur && trimIncludingNonbreakingSpace(text).length() > 0) {
            streetNumbersBuffer += " " + trimIncludingNonbreakingSpace(text);
        } else if (streetNumbersWillOccur && trimIncludingNonbreakingSpace(text).length() == 0) {
            poi.setStreetNumbers(streetNumbersBuffer);
            streetNumbersWillOccur = false;
        }

        if (text.contains("numery od")) {
            streetNumbersWillOccur = true;
            streetNumbersBuffer = text;
        }
    }*/

    
    public SearchResults getSearchResults(){
        return mSearchResults;
    }
    
    private String trimIncludingNonbreakingSpace(String s) {
        return s.replaceFirst("^[\\x00-\\x200\\xA0]+", "").replaceFirst("[\\x00-\\x20\\xA0]+$", "");
    }
}
